package chapter11;

import java.util.*;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/19 17:31
 */
public class QueueTest {
    // PriorityQueue实现的是最小堆，因此需要翻转比较结果
    private static Comparator<Task> taskComparator = ((o1, o2) -> {
        return Integer.compare(o2.getPriority(), o1.getPriority());
    });

    public static void main(String[] args) {
        Queue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
        pq.offer(0);
        pq.add(22);
        pq.addAll(Arrays.asList(11, 12, 34, 2, 7, 4, 15, 12, 8, 6, 19, 13));
        while (pq.peek() != null) {
            System.out.println(pq.poll());
        }

        Queue<Task> tasks = new PriorityQueue<>(taskComparator);
        tasks.offer(new Task(20, "写日记"));
        tasks.offer(new Task(10, "看电视"));
        tasks.offer(new Task(100, "写代码"));

        Task task = tasks.poll();
        while (task != null) {
            System.out.println("处理任务: " + task.getName() + ", 优先级:" + task.getPriority());
            task = tasks.poll();
        }
    }

    private static class Task {
        private int priority;
        private String name;

        public Task(int priority, String name) {
            this.priority = priority;
            this.name = name;
        }

        public int getPriority() {
            return priority;
        }

        public String getName() {
            return name;
        }
    }
}
